home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / modes / hideif.el.z / hideif.el
Encoding:
Text File  |  1998-05-21  |  32.7 KB  |  1,054 lines

  1. ;;; hide-ifdef-mode.el --- hides selected code within ifdef.
  2.  
  3. ;; Copyright (C) 1988, 1994 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Dan LaLiberte <liberte@a.cs.uiuc.edu>
  6. ;; Maintainer: FSF
  7. ;; Keywords: c, outlines
  8.  
  9. ;; This file is part of XEmacs.
  10.  
  11. ;; XEmacs is free software; you can redistribute it and/or modify it
  12. ;; under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation; either version 2, or (at your option)
  14. ;; any later version.
  15.  
  16. ;; XEmacs is distributed in the hope that it will be useful, but
  17. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  19. ;; General Public License for more details.
  20.  
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  23. ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  24. ;; 02111-1307, USA.
  25.  
  26. ;;; Synched up with: FSF 19.34.
  27.  
  28. ;;; Commentary:
  29.  
  30. ;; To initialize, toggle the hide-ifdef minor mode with
  31. ;;
  32. ;; M-x hide-ifdef-mode
  33. ;;
  34. ;; This will set up key bindings and call hide-ifdef-mode-hook if it
  35. ;; has a value.  To explicitly hide ifdefs using a buffer-local
  36. ;; define list (default empty), type
  37. ;;
  38. ;; M-x hide-ifdefs  or C-c @ h
  39. ;;
  40. ;; Hide-ifdef suppresses the display of code that the preprocessor wouldn't
  41. ;; pass through.  The support of constant expressions in #if lines is 
  42. ;; limited to identifiers, parens, and the operators: &&, ||, !, and
  43. ;; "defined".  Please extend this.
  44. ;;
  45. ;; The hidden code is marked by ellipses (...).  Be
  46. ;; cautious when editing near ellipses, since the hidden text is
  47. ;; still in the buffer, and you can move the point into it and modify
  48. ;; text unawares.  If you don't want to see the ellipses, set 
  49. ;; selective-display-ellipses to nil.  But this can be dangerous.
  50. ;; You can make your buffer read-only while hide-ifdef-hiding by setting
  51. ;; hide-ifdef-read-only to a non-nil value.  You can toggle this 
  52. ;; variable with hide-ifdef-toggle-read-only (C-c @ C-q).
  53. ;;
  54. ;; You can undo the effect of hide-ifdefs by typing
  55. ;;
  56. ;; M-x show-ifdefs  or C-c @ s
  57. ;;
  58. ;; Use M-x hide-ifdef-define (C-c @ d) to define a symbol.
  59. ;; Use M-x hide-ifdef-undef (C-c @ u) to undefine a symbol.
  60. ;;
  61. ;; If you define or undefine a symbol while hide-ifdef-mode is in effect,
  62. ;; the display will be updated.  Only the define list for the current
  63. ;; buffer will be affected.  You can save changes to the local define
  64. ;; list with hide-ifdef-set-define-alist.  This adds entries 
  65. ;; to hide-ifdef-define-alist.
  66. ;;
  67. ;; If you have defined a hide-ifdef-mode-hook, you can set
  68. ;; up a list of symbols that may be used by hide-ifdefs as in the
  69. ;; following example:
  70. ;;
  71. ;; (setq hide-ifdef-mode-hook
  72. ;;      '(lambda ()
  73. ;;     (if (not hide-ifdef-define-alist)
  74. ;;         (setq hide-ifdef-define-alist
  75. ;;          '((list1 ONE TWO)
  76. ;;            (list2 TWO THREE)
  77. ;;            )))
  78. ;;     (hide-ifdef-use-define-alist 'list2) ; use list2 by default
  79. ;;     ))
  80. ;;
  81. ;; You can call hide-ifdef-use-define-alist (C-c u) at any time to specify
  82. ;; another list to use.
  83. ;;
  84. ;; To cause ifdefs to be hidden as soon as hide-ifdef-mode is called,
  85. ;; set hide-ifdef-initially to non-nil.
  86. ;;
  87. ;; If you set hide-ifdef-lines to t, hide-ifdefs hides all the #ifdef lines.
  88. ;; In the absence of highlighting, that might be a bad idea.  If you set
  89. ;; hide-ifdef-lines to nil (the default), the surrounding preprocessor
  90. ;; lines will be displayed.  That can be confusing in its own
  91. ;; right.  Other variations on display are possible, but not much
  92. ;; better.
  93. ;;
  94. ;; You can explicitly hide or show individual ifdef blocks irrespective
  95. ;; of the define list by using hide-ifdef-block and show-ifdef-block.
  96. ;;
  97. ;; You can move the point between ifdefs with forward-ifdef, backward-ifdef,
  98. ;; up-ifdef, down-ifdef, next-ifdef, and previous-ifdef.
  99. ;;
  100. ;; If you have minor-mode-alist in your mode line (the default) two labels
  101. ;; may appear.  "Ifdef" will appear when hide-ifdef-mode is active.  "Hiding"
  102. ;; will appear when text may be hidden ("hide-ifdef-hiding" is non-nil).
  103. ;;
  104. ;; Written by Brian Marick, at Gould, Computer Systems Division, Urbana IL.
  105. ;; Extensively modified by Daniel LaLiberte (while at Gould).
  106. ;;
  107. ;; You may freely modify and distribute this, but keep a record
  108. ;; of modifications and send comments to:
  109. ;;      liberte@a.cs.uiuc.edu  or  ihnp4!uiucdcs!liberte
  110. ;; I will continue to upgrade hide-ifdef-mode
  111. ;; with your contributions.
  112.  
  113. ;;; Code:
  114.  
  115. (require 'cc-mode)
  116.  
  117. (defvar hide-ifdef-mode-submap nil
  118.   "Keymap used with Hide-Ifdef mode.")
  119.  
  120. (defvar hide-ifdef-mode-map nil
  121.   "Keymap used with Hide-Ifdef mode.")
  122.  
  123. (defconst hide-ifdef-mode-prefix-key "\C-c@"
  124.   "Prefix key for all Hide-Ifdef mode commands.")
  125.  
  126. ;; Set up the submap that goes after the prefix key.
  127. (if hide-ifdef-mode-submap
  128.     ()                ; Don't redefine it.
  129.   (setq hide-ifdef-mode-submap (make-sparse-keymap))
  130.   (define-key hide-ifdef-mode-submap "d" 'hide-ifdef-define)
  131.   (define-key hide-ifdef-mode-submap "u" 'hide-ifdef-undef)
  132.   (define-key hide-ifdef-mode-submap "D" 'hide-ifdef-set-define-alist)
  133.   (define-key hide-ifdef-mode-submap "U" 'hide-ifdef-use-define-alist)
  134.  
  135.   (define-key hide-ifdef-mode-submap "h" 'hide-ifdefs)
  136.   (define-key hide-ifdef-mode-submap "s" 'show-ifdefs)
  137.   (define-key hide-ifdef-mode-submap "\C-d" 'hide-ifdef-block)
  138.   (define-key hide-ifdef-mode-submap "\C-s" 'show-ifdef-block)
  139.  
  140.   (define-key hide-ifdef-mode-submap "\C-q" 'hide-ifdef-toggle-read-only)
  141.   (let ((where (where-is-internal 'toggle-read-only nil t)))
  142.     (if where
  143.     (define-key hide-ifdef-mode-submap
  144.       where
  145.       'hide-ifdef-toggle-outside-read-only)))
  146.   )
  147.  
  148. ;; Set up the mode's main map, which leads via the prefix key to the submap.
  149. (if hide-ifdef-mode-map
  150.     ()
  151.   (setq hide-ifdef-mode-map (make-sparse-keymap))
  152.   (define-key hide-ifdef-mode-map hide-ifdef-mode-prefix-key
  153.     hide-ifdef-mode-submap))
  154.  
  155. (defvar hide-ifdef-mode nil
  156.   "Non-nil when hide-ifdef-mode is activated.")
  157.  
  158. (defvar hide-ifdef-hiding nil
  159.   "Non-nil when text may be hidden.")
  160.  
  161. (or (assq 'hide-ifdef-hiding minor-mode-alist)
  162.     (setq minor-mode-alist
  163.           (cons '(hide-ifdef-hiding " Hiding")
  164.                 minor-mode-alist)))
  165.  
  166. ;(or (assq 'hide-ifdef-mode minor-mode-alist)
  167. ;    (setq minor-mode-alist
  168. ;          (cons '(hide-ifdef-mode " Ifdef")
  169. ;                minor-mode-alist)))
  170. ;; XEmacs: do it right.
  171. ;;;###autoload
  172. (add-minor-mode 'hide-ifdef-mode " Ifdef" 'hide-ifdef-mode-map)
  173.  
  174. ;; fix c-mode syntax table so we can recognize whole symbols.
  175. (defvar hide-ifdef-syntax-table
  176.   (copy-syntax-table c-mode-syntax-table)
  177.   "Syntax table used for tokenizing #if expressions.")
  178.  
  179. (modify-syntax-entry ?_ "w" hide-ifdef-syntax-table)
  180. (modify-syntax-entry ?& "." hide-ifdef-syntax-table)
  181. (modify-syntax-entry ?\| "." hide-ifdef-syntax-table)
  182.  
  183. ;;;###autoload
  184. (defun hide-ifdef-mode (arg)
  185.   "Toggle Hide-Ifdef mode.  This is a minor mode, albeit a large one.
  186. With ARG, turn Hide-Ifdef mode on if arg is positive, off otherwise.
  187. In Hide-Ifdef mode, code within #ifdef constructs that the C preprocessor
  188. would eliminate may be hidden from view.  Several variables affect
  189. how the hiding is done:
  190.  
  191. hide-ifdef-env
  192.     An association list of defined and undefined symbols for the
  193.     current buffer.  Initially, the global value of `hide-ifdef-env'
  194.     is used.
  195.  
  196. hide-ifdef-define-alist
  197.     An association list of defined symbol lists.  
  198.         Use `hide-ifdef-set-define-alist' to save the current `hide-ifdef-env'
  199.         and `hide-ifdef-use-define-alist' to set the current `hide-ifdef-env'
  200.         from one of the lists in `hide-ifdef-define-alist'.
  201.  
  202. hide-ifdef-lines
  203.     Set to non-nil to not show #if, #ifdef, #ifndef, #else, and
  204.     #endif lines when hiding.
  205.  
  206. hide-ifdef-initially
  207.     Indicates whether `hide-ifdefs' should be called when Hide-Ifdef mode
  208.     is activated.
  209.  
  210. hide-ifdef-read-only
  211.     Set to non-nil if you want to make buffers read only while hiding.
  212.     After `show-ifdefs', read-only status is restored to previous value.
  213.  
  214. \\{hide-ifdef-mode-map}"
  215.  
  216.   (interactive "P")
  217.   (make-local-variable 'hide-ifdef-mode)
  218.   (setq hide-ifdef-mode
  219.     (if (null arg)
  220.         (not hide-ifdef-mode)
  221.       (> (prefix-numeric-value arg) 0)))
  222.   
  223.   ;; XEmacs change
  224.   (redraw-modeline)
  225.  
  226.   (if hide-ifdef-mode
  227.       (progn
  228.     ; fix c-mode syntax table so we can recognize whole symbols.
  229.     ;; XEmacs:  Maybe we don't need this any more with cc-mode? -sb
  230.     ;; (modify-syntax-entry ?_ "w")
  231.     ;; (modify-syntax-entry ?& ".")
  232.     ;; (modify-syntax-entry ?\| ".")
  233.  
  234.     ; inherit global values
  235.     (make-local-variable 'hide-ifdef-env)
  236.     (setq hide-ifdef-env (default-value 'hide-ifdef-env))
  237.  
  238.     (make-local-variable 'hide-ifdef-hiding)
  239.     (setq hide-ifdef-hiding (default-value 'hide-ifdef-hiding))
  240.  
  241.     (make-local-variable 'hif-outside-read-only)
  242.     (setq hif-outside-read-only buffer-read-only)
  243.  
  244.     (run-hooks 'hide-ifdef-mode-hook)
  245.  
  246.     (if hide-ifdef-initially
  247.         (hide-ifdefs)
  248.       (show-ifdefs))
  249.     (message "Enter Hide-Ifdef mode")
  250.     )
  251.      ; else end hide-ifdef-mode
  252.     (if hide-ifdef-hiding
  253.     (show-ifdefs))
  254.     (message "Exit Hide-Ifdef mode")
  255.     ))
  256.   
  257.  
  258. ;; from outline.el with docstring fixed.
  259. (defun hif-outline-flag-region (from to flag)
  260.   "Hides or shows lines from FROM to TO, according to FLAG.
  261. If FLAG is \\n (newline character) then text is shown, while if FLAG is \\^M
  262. \(control-M) the text is hidden."
  263.   (let ((modp (buffer-modified-p)))
  264.     (unwind-protect (progn
  265.               (subst-char-in-region from to
  266.                   (if (= flag ?\n) ?\^M ?\n)
  267.                   flag t) )
  268.       (set-buffer-modified-p modp))
  269.     ))
  270.  
  271. (defun hif-show-all ()
  272.   "Show all of the text in the current buffer."
  273.   (interactive)
  274.   (hif-outline-flag-region (point-min) (point-max) ?\n))
  275.  
  276. ;; By putting this on after-revert-hook, we arrange that it only
  277. ;; does anything when revert-buffer avoids turning off the mode.
  278. ;; (That can happen in VC.)
  279. (defun hif-before-revert-function ()
  280.   (and hide-ifdef-mode hide-ifdef-hiding
  281.        (hide-ifdefs t)))
  282. (add-hook 'after-revert-hook 'hif-before-revert-function)
  283.  
  284. (defun hide-ifdef-region (start end)
  285.   "START is the start of a #if or #else form.  END is the ending part.
  286. Everything including these lines is made invisible."
  287.   (hif-outline-flag-region start end ?\^M)
  288.   )
  289.  
  290. (defun hif-show-ifdef-region (start end)
  291.   "Everything between START and END is made visible."
  292.   (hif-outline-flag-region start end ?\n)
  293.   )
  294.  
  295.  
  296.  
  297. ;===%%SF%% evaluation (Start)  ===
  298.  
  299. ;; It is not useful to set this to anything but `eval'.
  300. ;; In fact, the variable might as well be eliminated.
  301. (defvar hide-ifdef-evaluator 'eval
  302.   "The function to use to evaluate a form.
  303. The evaluator is given a canonical form and returns t if text under
  304. that form should be displayed.")
  305.  
  306. (defvar hif-undefined-symbol nil
  307.   "...is by default considered to be false.")
  308.  
  309. (defvar hide-ifdef-env nil
  310.   "An alist of defined symbols and their values.")
  311.  
  312.  
  313. (defun hif-set-var (var value)
  314.   "Prepend (var value) pair to hide-ifdef-env."
  315.   (setq hide-ifdef-env (cons (cons var value) hide-ifdef-env)))
  316.  
  317.  
  318. (defun hif-lookup (var)
  319. ;  (message "hif-lookup %s" var)
  320.   (let ((val (assoc var hide-ifdef-env)))
  321.     (if val
  322.     (cdr val)
  323.       hif-undefined-symbol)))
  324.  
  325. (defun hif-defined (var)
  326.   (hif-lookup var)
  327.   ; when #if expressions are fully supported, defined result should be 1
  328.   ;  (if (assoc var  hide-ifdef-env)
  329.   ;      1
  330.   ;    nil)
  331. )
  332.  
  333.  
  334. ;===%%SF%% evaluation (End)  ===
  335.  
  336.  
  337.  
  338. ;===%%SF%% parsing (Start)  ===
  339. ;;;  The code that understands what ifs and ifdef in files look like.
  340.  
  341. (defconst hif-cpp-prefix "\\(^\\|\r\\)[ \t]*#[ \t]*")
  342. (defconst hif-ifndef-regexp (concat hif-cpp-prefix "ifndef"))
  343. (defconst hif-ifx-regexp (concat hif-cpp-prefix "if\\(n?def\\)?[ \t]+"))
  344. (defconst hif-else-regexp (concat hif-cpp-prefix "else"))
  345. (defconst hif-endif-regexp (concat hif-cpp-prefix "endif"))
  346. (defconst hif-ifx-else-endif-regexp
  347.   (concat hif-ifx-regexp "\\|" hif-else-regexp "\\|" hif-endif-regexp))
  348.  
  349.  
  350. (defun hif-infix-to-prefix (token-list)
  351.   "Convert list of tokens in infix into prefix list"
  352. ;  (message "hif-infix-to-prefix: %s" token-list)
  353.   (if (= 1 (length token-list))
  354.       (` (hif-lookup (quote (, (car token-list)))))
  355.     (hif-parse-if-exp token-list))
  356.   )
  357.  
  358. ; pattern to match initial identifier, !, &&, ||, (, or ).
  359. ; Added ==, + and -: garyo@avs.com 8/9/94
  360. (defconst hif-token-regexp "^\\(!\\|&&\\|||\\|[!=]=\\|[()+-]\\|\\w+\\)")
  361. (defconst hif-end-of-comment "\\*/")
  362.  
  363.  
  364. (defun hif-tokenize (expr-string)
  365.   "Separate string into a list of tokens"
  366.   (let ((token-list nil)
  367.     (expr-start 0)
  368.     (expr-length (length expr-string))
  369.     (current-syntax-table (syntax-table)))
  370.     (unwind-protect
  371.     (progn
  372.       (set-syntax-table hide-ifdef-syntax-table)
  373.       (while (< expr-start expr-length) 
  374. ;        (message "expr-start = %d" expr-start) (sit-for 1)
  375.         (cond
  376.          ((string-match "^[ \t]+" expr-string expr-start)
  377.           ;; skip whitespace
  378.           (setq expr-start (match-end 0))
  379.           ;; stick newline in string so ^ matches on the next string-match
  380.           (aset expr-string (1- expr-start) ?\n))
  381.  
  382.          ((string-match "^/\\*" expr-string expr-start)
  383.           (setq expr-start (match-end 0))
  384.           (aset expr-string (1- expr-start) ?\n)
  385.           (or
  386.            (string-match hif-end-of-comment
  387.                  expr-string expr-start) ; eat comment
  388.            (string-match "$" expr-string expr-start)) ; multi-line comment
  389.           (setq expr-start (match-end 0))
  390.           (aset expr-string (1- expr-start) ?\n))
  391.  
  392.          ((string-match "^//" expr-string expr-start)
  393.           (string-match "$" expr-string expr-start)
  394.           (setq expr-start (match-end 0)))
  395.  
  396.          ((string-match hif-token-regexp expr-string expr-start)
  397.           (let ((token (substring expr-string expr-start (match-end 0))))
  398.         (setq expr-start (match-end 0))
  399.         (aset expr-string (1- expr-start) ?\n)
  400. ;        (message "token: %s" token) (sit-for 1)
  401.         (setq token-list
  402.               (cons
  403.                (cond
  404.             ((string-equal token "||") 'or)
  405.             ((string-equal token "&&") 'and)
  406.             ((string-equal token "==") 'equal)
  407.             ((string-equal token "!=") 'hif-notequal)
  408.             ((string-equal token "!")  'not)
  409.             ((string-equal token "defined") 'hif-defined)
  410.             ((string-equal token "(") 'lparen)
  411.             ((string-equal token ")") 'rparen)
  412.             ((string-equal token "+") 'hif-plus)
  413.             ((string-equal token "-") 'hif-minus)
  414.             (t (intern token)))
  415.                token-list))))
  416.          (t (error "Bad #if expression: %s" expr-string)))))
  417.       (set-syntax-table current-syntax-table))
  418.     (nreverse token-list)))
  419.  
  420. ;;;-----------------------------------------------------------------
  421. ;;; Translate C preprocessor #if expressions using recursive descent.
  422. ;;; This parser is limited to the operators &&, ||, !, and "defined".
  423. ;;; Added ==, !=, +, and -.  Gary Oberbrunner, garyo@avs.com, 8/9/94
  424.  
  425. (defun hif-parse-if-exp (token-list)
  426.   "Parse the TOKEN-LIST.  Return translated list in prefix form."
  427.   (hif-nexttoken)
  428.   (prog1
  429.       (hif-expr)
  430.     (if token ; is there still a token?
  431.     (error "Error: unexpected token: %s" token))))
  432.  
  433. (defun hif-nexttoken ()
  434.   "Pop the next token from token-list into the let variable \"token\"."
  435.   (setq token (car token-list))
  436.   (setq token-list (cdr token-list))
  437.   token)
  438.  
  439. (defun hif-expr ()
  440.   "Parse an expression as found in #if.
  441.        expr : term | expr '||' term."
  442.   (let ((result (hif-term)))
  443.     (while (eq  token 'or)
  444.       (hif-nexttoken)
  445.       (setq result (list 'or result (hif-term))))
  446.   result))
  447.  
  448. (defun hif-term ()
  449.   "Parse a term : eq-expr | term '&&' eq-expr."
  450.   (let ((result (hif-eq-expr)))
  451.     (while (eq token 'and)
  452.       (hif-nexttoken)
  453.       (setq result (list 'and result (hif-eq-expr))))
  454.     result))
  455.  
  456. (defun hif-eq-expr ()
  457.   "Parse an eq-expr : math | eq-expr '=='|'!=' math."
  458.   (let ((result (hif-math))
  459.     (eq-token nil))
  460.     (while (or (eq token 'equal) (eq token 'hif-notequal))
  461.       (setq eq-token token)
  462.       (hif-nexttoken)
  463.       (setq result (list eq-token result (hif-math))))
  464.     result))
  465.  
  466. (defun hif-math ()
  467.   "Parse an expression with + or - and simpler things.
  468.        math : factor | math '+|-' factor."
  469.   (let ((result (hif-factor))
  470.     (math-op nil))
  471.     (while (or (eq  token 'hif-plus) (eq token 'hif-minus))
  472.       (setq math-op token)
  473.       (hif-nexttoken)
  474.       (setq result (list math-op result (hif-factor))))
  475.   result))
  476.  
  477. (defun hif-factor ()
  478.   "Parse a factor: '!' factor | '(' expr ')' | 'defined(' id ')' | id."
  479.   (cond
  480.     ((eq token 'not)
  481.      (hif-nexttoken)
  482.      (list 'not (hif-factor)))
  483.  
  484.     ((eq token 'lparen)
  485.      (hif-nexttoken)
  486.      (let ((result (hif-expr)))
  487.        (if (not (eq token 'rparen))
  488.        (error "Bad token in parenthesized expression: %s" token)
  489.      (hif-nexttoken)
  490.      result)))
  491.  
  492.     ((eq token 'hif-defined)
  493.      (hif-nexttoken)
  494.      (if (not (eq token 'lparen))
  495.      (error "Error: expected \"(\" after \"defined\""))
  496.      (hif-nexttoken)
  497.      (let ((ident token))
  498.        (if (memq token '(or and not hif-defined lparen rparen))
  499.        (error "Error: unexpected token: %s" token))
  500.        (hif-nexttoken)
  501.        (if (not (eq token 'rparen))
  502.        (error "Error: expected \")\" after identifier"))
  503.        (hif-nexttoken)
  504.        (` (hif-defined (quote (, ident))))
  505.        ))
  506.  
  507.     (t ; identifier
  508.       (let ((ident token))
  509.     (if (memq ident '(or and))
  510.         (error "Error: missing identifier"))
  511.     (hif-nexttoken)
  512.     (` (hif-lookup (quote (, ident))))
  513.     ))
  514.     ))
  515.  
  516. (defun hif-mathify (val)
  517.   "Treat VAL as a number: if it's t or nil, use 1 or 0."
  518.   (cond ((eq val t)
  519.      1)
  520.     ((null val)
  521.      0)
  522.     (t val)))
  523.  
  524. (defun hif-plus (a b)
  525.   "Like ordinary plus but treat t and nil as 1 and 0."
  526.   (+ (hif-mathify a) (hif-mathify b)))
  527. (defun hif-minus (a b)
  528.   "Like ordinary minus but treat t and nil as 1 and 0."
  529.   (- (hif-mathify a) (hif-mathify b)))
  530. (defun hif-notequal (a b)
  531.   "Like (not (equal A B)) but as one symbol."
  532.   (not (equal a b)))
  533.  
  534. ;;;----------- end of parser -----------------------
  535.  
  536.  
  537. (defun hif-canonicalize ()
  538.   "When at beginning of #ifX, returns a Lisp expression for its condition."
  539.   (save-excursion
  540.     (let ((negate (looking-at hif-ifndef-regexp)))
  541.       (re-search-forward hif-ifx-regexp)
  542.       (let* ((expr-string
  543.           (buffer-substring (point)
  544.                 (progn (skip-chars-forward "^\n\r") (point))))
  545.          (expr (hif-infix-to-prefix (hif-tokenize expr-string))))
  546. ;    (message "hif-canonicalized: %s" expr)
  547.     (if negate
  548.         (list 'not expr)
  549.       expr)))))
  550.  
  551.  
  552. (defun hif-find-any-ifX ()
  553.   "Move to next #if..., or #ifndef, at point or after."
  554. ;  (message "find ifX at %d" (point))
  555.   (prog1
  556.       (re-search-forward hif-ifx-regexp (point-max) t)
  557.     (beginning-of-line)))
  558.  
  559.  
  560. (defun hif-find-next-relevant ()
  561.   "Move to next #if..., #else, or #endif, after the current line."
  562. ;  (message "hif-find-next-relevant at %d" (point))
  563.   (end-of-line)
  564.   ; avoid infinite recursion by only going to beginning of line if match found
  565.   (if (re-search-forward hif-ifx-else-endif-regexp (point-max) t)
  566.       (beginning-of-line)))
  567.  
  568. (defun hif-find-previous-relevant ()
  569.   "Move to previous #if..., #else, or #endif, before the current line."
  570. ;  (message "hif-find-previous-relevant at %d" (point))
  571.   (beginning-of-line)
  572.   ; avoid infinite recursion by only going to beginning of line if match found
  573.   (if (re-search-backward hif-ifx-else-endif-regexp (point-min) t)
  574.      (beginning-of-line)))
  575.  
  576.  
  577. (defun hif-looking-at-ifX ()        ;; Should eventually see #if
  578.   (looking-at hif-ifx-regexp))
  579. (defun hif-looking-at-endif ()
  580.   (looking-at hif-endif-regexp))
  581. (defun hif-looking-at-else ()
  582.   (looking-at hif-else-regexp))
  583.  
  584.  
  585.  
  586. (defun hif-ifdef-to-endif ()
  587.   "If positioned at #ifX or #else form, skip to corresponding #endif."
  588. ;  (message "hif-ifdef-to-endif at %d" (point)) (sit-for 1)
  589.   (hif-find-next-relevant)
  590.   (cond ((hif-looking-at-ifX)
  591.      (hif-ifdef-to-endif) ; find endif of nested if
  592.      (hif-ifdef-to-endif)) ; find outer endif or else
  593.     ((hif-looking-at-else)
  594.      (hif-ifdef-to-endif)) ; find endif following else
  595.     ((hif-looking-at-endif)
  596.      'done)
  597.     (t
  598.      (error "Mismatched #ifdef #endif pair"))))
  599.  
  600.  
  601. (defun hif-endif-to-ifdef ()
  602.   "If positioned at #endif form, skip backward to corresponding #ifX."
  603. ;  (message "hif-endif-to-ifdef at %d" (point))
  604.   (let ((start (point)))
  605.     (hif-find-previous-relevant)
  606.     (if (= start (point))
  607.     (error "Mismatched #ifdef #endif pair")))
  608.   (cond ((hif-looking-at-endif)
  609.      (hif-endif-to-ifdef) ; find beginning of nested if
  610.      (hif-endif-to-ifdef)) ; find beginning of outer if or else
  611.     ((hif-looking-at-else)
  612.      (hif-endif-to-ifdef))
  613.     ((hif-looking-at-ifX)
  614.      'done)
  615.     (t)))            ; never gets here
  616.  
  617.  
  618. (defun forward-ifdef (&optional arg)
  619.   "Move point to beginning of line of the next ifdef-endif.
  620. With argument, do this that many times."
  621.   (interactive "p")
  622.   (or arg (setq arg 1))
  623.   (if (< arg 0)
  624.       (backward-ifdef (- arg)))
  625.   (while (< 0 arg)
  626.     (setq arg (- arg))
  627.     (let ((start (point)))
  628.       (if (not (hif-looking-at-ifX))
  629.       (hif-find-next-relevant))
  630.       (if (hif-looking-at-ifX)
  631.       (hif-ifdef-to-endif)
  632.     (goto-char start)
  633.     (error "No following #ifdef")
  634.     ))))
  635.  
  636.  
  637. (defun backward-ifdef (&optional arg)
  638.   "Move point to beginning of the previous ifdef-endif.
  639. With argument, do this that many times."
  640.   (interactive "p")
  641.   (or arg (setq arg 1))
  642.   (if (< arg 0)
  643.       (forward-ifdef (- arg)))
  644.   (while (< 0 arg)
  645.     (setq arg (1- arg))
  646.     (beginning-of-line)
  647.     (let ((start (point)))
  648.       (if (not (hif-looking-at-endif))
  649.       (hif-find-previous-relevant))
  650.       (if (hif-looking-at-endif)
  651.       (hif-endif-to-ifdef)
  652.     (goto-char start)
  653.     (error "No previous #ifdef")))))
  654.  
  655.  
  656. (defun down-ifdef ()
  657.   "Move point to beginning of nested ifdef or else-part."
  658.     (interactive)
  659.     (let ((start (point)))
  660.       (hif-find-next-relevant)
  661.       (if (or (hif-looking-at-ifX) (hif-looking-at-else))
  662.       ()
  663.     (goto-char start)
  664.     (error "No following #ifdef"))))
  665.  
  666.  
  667. (defun up-ifdef ()
  668.   "Move point to beginning of enclosing ifdef or else-part."
  669.   (interactive)
  670.   (beginning-of-line)
  671.   (let ((start (point)))
  672.     (if (not (hif-looking-at-endif))
  673.     (hif-find-previous-relevant))
  674.     (if (hif-looking-at-endif)
  675.     (hif-endif-to-ifdef))
  676.       (if (= start (point))
  677.       (error "No previous #ifdef"))))
  678.  
  679. (defun next-ifdef (&optional arg)
  680.   "Move to the beginning of the next #ifX, #else, or #endif.
  681. With argument, do this that many times."
  682.   (interactive "p")
  683.   (or arg (setq arg 1))
  684.   (if (< arg 0)
  685.       (previous-ifdef (- arg)))
  686.   (while (< 0 arg)
  687.     (setq arg (1- arg))
  688.     (hif-find-next-relevant)
  689.     (if (eolp)
  690.     (progn
  691.       (beginning-of-line)
  692.       (error "No following #ifdefs, #elses, or #endifs")))))
  693.  
  694. (defun previous-ifdef (&optional arg)
  695.   "Move to the beginning of the previous #ifX, #else, or #endif.
  696. With argument, do this that many times."
  697.   (interactive "p")
  698.   (or arg (setq arg 1))
  699.   (if (< arg 0)
  700.       (next-ifdef (- arg)))
  701.   (while (< 0 arg)
  702.     (setq arg (1- arg))
  703.     (let ((start (point)))
  704.       (hif-find-previous-relevant)
  705.       (if (= start (point))
  706.       (error "No previous #ifdefs, #elses, or #endifs")))))
  707.  
  708.  
  709. ;===%%SF%% parsing (End)  ===
  710.  
  711.  
  712. ;===%%SF%% hide-ifdef-hiding (Start)  ===
  713.  
  714.  
  715. ;;; A range is a structure with four components:
  716. ;;; ELSE-P    True if there was an else clause for the ifdef.
  717. ;;; START    The start of the range. (beginning of line)
  718. ;;; ELSE    The else marker (beginning of line)
  719. ;;;            Only valid if ELSE-P is true.
  720. ;;; END        The end of the range.  (beginning of line)
  721.  
  722. (defun hif-make-range (else-p start end &optional else)
  723.   (list else-p start else end))
  724.  
  725. (defun hif-range-else-p (range)  (elt range 0))
  726. (defun hif-range-start (range) (elt range 1))
  727. (defun hif-range-else (range) (elt range 2))
  728. (defun hif-range-end (range) (elt range 3))
  729.  
  730.  
  731.  
  732. ;;; Find-Range
  733. ;;; The workhorse, it delimits the #if region.  Reasonably simple:
  734. ;;; Skip until an #else or #endif is found, remembering positions.  If
  735. ;;; an #else was found, skip some more, looking for the true #endif.
  736.  
  737. (defun hif-find-range ()
  738.   "Returns a Range structure describing the current #if region.
  739. Point is left unchanged."
  740. ;  (message "hif-find-range at %d" (point))
  741.   (save-excursion
  742.     (beginning-of-line)
  743.     (let ((start (point))
  744.       (else-p nil)
  745.       (else nil)
  746.       (end nil))
  747.       ;; Part one.  Look for either #endif or #else.
  748.       ;; This loop-and-a-half dedicated to E. Dijkstra.
  749.       (hif-find-next-relevant)
  750.       (while (hif-looking-at-ifX)        ; Skip nested ifdef
  751.     (hif-ifdef-to-endif)
  752.     (hif-find-next-relevant))
  753.       ;; Found either a #else or an #endif.
  754.       (cond ((hif-looking-at-else)
  755.          (setq else-p t)
  756.          (setq else (point)))
  757.         (t
  758.          (setq end (point)) ; (save-excursion (end-of-line) (point))
  759.          ))
  760.       ;; If found #else, look for #endif.
  761.       (if else-p
  762.       (progn
  763.         (hif-find-next-relevant)
  764.         (while (hif-looking-at-ifX)    ; Skip nested ifdef
  765.           (hif-ifdef-to-endif)
  766.           (hif-find-next-relevant))
  767.         (if (hif-looking-at-else)
  768.         (error "Found two elses in a row?  Broken!"))
  769.         (setq end (point))  ; (save-excursion (end-of-line) (point))
  770.         ))
  771.       (hif-make-range else-p start end else))))
  772.  
  773.       
  774. ;;; A bit slimy.
  775. ;;; NOTE:  If there's an #ifdef at the beginning of the file, we can't
  776. ;;; hide it.  There's no previous newline to replace.  If we added
  777. ;;; one, we'd throw off all the counts.  Feh.
  778.  
  779. (defun hif-hide-line (point)
  780.   "Hide the line containing point.  Does nothing if `hide-ifdef-lines' is nil."
  781.   (if hide-ifdef-lines
  782.       (save-excursion
  783.     (goto-char point)
  784.     (let ((modp (buffer-modified-p)))
  785.       (unwind-protect
  786.           (progn
  787.         (beginning-of-line)
  788.         (if (not (= (point) 1))
  789.             (hide-ifdef-region (1- (point)) (point))))
  790.         (set-buffer-modified-p modp))
  791.       ))
  792.     ))
  793.           
  794.  
  795. ;;;  Hif-Possibly-Hide
  796. ;;;  There are four cases.  The #ifX expression is "taken" if it
  797. ;;;  the hide-ifdef-evaluator returns T.  Presumably, this means the code
  798. ;;;  inside the #ifdef would be included when the program was
  799. ;;;  compiled.  
  800. ;;;
  801. ;;;  Case 1:  #ifX taken, and there's an #else.
  802. ;;;    The #else part must be hidden.  The #if (then) part must be
  803. ;;;    processed for nested #ifX's.
  804. ;;;  Case 2:  #ifX taken, and there's no #else.
  805. ;;;    The #if part must be processed for nested #ifX's.
  806. ;;;  Case 3:  #ifX not taken, and there's an #else.
  807. ;;;    The #if part must be hidden.  The #else part must be processed
  808. ;;;    for nested #ifs.
  809. ;;;  Case 4:  #ifX not taken, and there's no #else.
  810. ;;;    The #ifX part must be hidden.
  811. ;;;
  812. ;;;  Further processing is done by narrowing to the relevant region
  813. ;;;  and just recursively calling hide-ifdef-guts.
  814. ;;;
  815. ;;;  When hif-possibly-hide returns, point is at the end of the
  816. ;;;  possibly-hidden range.
  817.  
  818. (defun hif-recurse-on (start end)
  819.   "Call `hide-ifdef-guts' after narrowing to end of START line and END line."
  820.   (save-excursion
  821.     (save-restriction
  822.       (goto-char start)
  823.       (end-of-line)
  824.       (narrow-to-region (point) end)
  825.       (hide-ifdef-guts))))
  826.  
  827. (defun hif-possibly-hide ()
  828.   "Called at #ifX expression, this hides those parts that should be hidden.
  829. It uses the judgement of `hide-ifdef-evaluator'."
  830. ;  (message "hif-possibly-hide") (sit-for 1)
  831.     (let ((test (hif-canonicalize))
  832.       (range (hif-find-range)))
  833. ;      (message "test = %s" test) (sit-for 1)
  834.       
  835.       (hif-hide-line (hif-range-end range))
  836.       (if (funcall hide-ifdef-evaluator test)
  837.       (cond ((hif-range-else-p range) ; case 1
  838.          (hif-hide-line (hif-range-else range))
  839.          (hide-ifdef-region (hif-range-else range) 
  840.                     (1- (hif-range-end range)))
  841.          (hif-recurse-on (hif-range-start range)
  842.                  (hif-range-else range)))
  843.         (t ; case 2
  844.          (hif-recurse-on (hif-range-start range)
  845.                  (hif-range-end range))))
  846.     (cond ((hif-range-else-p range) ; case 3
  847.            (hif-hide-line (hif-range-else range))
  848.            (hide-ifdef-region (hif-range-start range)
  849.                   (1- (hif-range-else range)))
  850.            (hif-recurse-on (hif-range-else range)
  851.                    (hif-range-end range)))
  852.           (t ; case 4
  853.            (hide-ifdef-region (point)
  854.                   (1- (hif-range-end range))))
  855.           ))
  856.       (hif-hide-line (hif-range-start range))    ; Always hide start.
  857.       (goto-char (hif-range-end range))
  858.       (end-of-line)
  859.       ))
  860.  
  861.  
  862.  
  863. (defun hide-ifdef-guts ()
  864.   "Does most of the work of `hide-ifdefs'.
  865. It does not do the work that's pointless to redo on a recursive entry."
  866. ;  (message "hide-ifdef-guts")
  867.   (save-excursion
  868.     (goto-char (point-min))
  869.     (while (hif-find-any-ifX)
  870.       (hif-possibly-hide))))
  871.  
  872. ;===%%SF%% hide-ifdef-hiding (End)  ===
  873.  
  874.  
  875. ;===%%SF%% exports (Start)  ===
  876.  
  877. ;;;###autoload
  878. (defvar hide-ifdef-initially nil
  879.   "*Non-nil means call `hide-ifdefs' when Hide-Ifdef mode is first activated.")
  880.  
  881. ;;;###autoload
  882. (defvar hide-ifdef-read-only nil
  883.   "*Set to non-nil if you want buffer to be read-only while hiding text.")
  884.  
  885. (defvar hif-outside-read-only nil
  886.   "Internal variable.  Saves the value of `buffer-read-only' while hiding.")
  887.  
  888. ;;;###autoload
  889. (defvar hide-ifdef-lines nil
  890.   "*Non-nil means hide the #ifX, #else, and #endif lines.")
  891.  
  892. (defun hide-ifdef-toggle-read-only ()
  893.   "Toggle hide-ifdef-read-only."
  894.   (interactive)
  895.   (setq hide-ifdef-read-only (not hide-ifdef-read-only))
  896.   (message "Hide-Read-Only %s"
  897.        (if hide-ifdef-read-only "ON" "OFF"))
  898.   (if hide-ifdef-hiding
  899.       (setq buffer-read-only (or hide-ifdef-read-only hif-outside-read-only)))
  900.   ;; XEmacs change
  901.   (redraw-modeline))
  902.  
  903. (defun hide-ifdef-toggle-outside-read-only ()
  904.   "Replacement for `toggle-read-only' within Hide-Ifdef mode."
  905.   (interactive)
  906.   (setq hif-outside-read-only (not hif-outside-read-only))
  907.   (message "Read only %s"
  908.        (if hif-outside-read-only "ON" "OFF"))
  909.   (setq buffer-read-only
  910.     (or (and hide-ifdef-hiding hide-ifdef-read-only)
  911.         hif-outside-read-only)
  912.     )
  913.   ;; XEmacs change
  914.   (redraw-modeline))
  915.  
  916.       
  917. (defun hide-ifdef-define (var)
  918.   "Define a VAR so that #ifdef VAR would be included."
  919.   (interactive "SDefine what? ")
  920.   (hif-set-var var 1)
  921.   (if hide-ifdef-hiding (hide-ifdefs)))
  922.  
  923. (defun hide-ifdef-undef (var)
  924.   "Undefine a VAR so that #ifdef VAR would not be included."
  925.   (interactive "SUndefine what? ")
  926.   (hif-set-var var nil)
  927.   (if hide-ifdef-hiding (hide-ifdefs)))
  928.  
  929.  
  930. (defun hide-ifdefs (&optional nomsg)
  931.   "Hide the contents of some #ifdefs.  
  932. Assume that defined symbols have been added to `hide-ifdef-env'.  
  933. The text hidden is the text that would not be included by the C
  934. preprocessor if it were given the file with those symbols defined.
  935.  
  936. Turn off hiding by calling `show-ifdefs'."
  937.  
  938.   (interactive)
  939.   (message "Hiding...")
  940.   (setq hif-outside-read-only buffer-read-only)
  941.   (if (not hide-ifdef-mode)
  942.       (hide-ifdef-mode 1)) ; turn on hide-ifdef-mode
  943.   (if hide-ifdef-hiding
  944.       (show-ifdefs))            ; Otherwise, deep confusion.
  945.   (let ((inhibit-read-only t))
  946.     (setq selective-display t)
  947.     (setq hide-ifdef-hiding t)
  948.     (hide-ifdef-guts))
  949.   (setq buffer-read-only (or hide-ifdef-read-only hif-outside-read-only))
  950.   (or nomsg
  951.       (message "Hiding done")))
  952.  
  953.  
  954. (defun show-ifdefs ()
  955.   "Cancel the effects of `hide-ifdef': show the contents of all #ifdefs."
  956.   (interactive)
  957.   (setq buffer-read-only hif-outside-read-only)
  958.   (setq selective-display nil)    ; defaults
  959.   (let ((inhibit-read-only t))
  960.     (hif-show-all))
  961.   (setq hide-ifdef-hiding nil))
  962.  
  963.  
  964. (defun hif-find-ifdef-block ()
  965.   "Utility for hide and show `ifdef-block'.
  966. Set top and bottom of ifdef block."
  967.   (let (max-bottom)
  968.   (save-excursion
  969.     (beginning-of-line)
  970.     (if (not (or (hif-looking-at-else) (hif-looking-at-ifX)))
  971.     (up-ifdef))
  972.     (setq top (point))
  973.     (hif-ifdef-to-endif)
  974.     (setq max-bottom (1- (point))))
  975.   (save-excursion
  976.     (beginning-of-line)
  977.     (if (not (hif-looking-at-endif))
  978.     (hif-find-next-relevant))
  979.     (while (hif-looking-at-ifX)
  980.       (hif-ifdef-to-endif)
  981.       (hif-find-next-relevant))
  982.     (setq bottom (min max-bottom (1- (point)))))))
  983.  
  984.  
  985. (defun hide-ifdef-block ()
  986.   "Hide the ifdef block (true or false part) enclosing or before the cursor."
  987.   (interactive)
  988.   (if (not hide-ifdef-mode)
  989.       (hide-ifdef-mode 1))
  990.   (setq selective-display t)
  991.   (let (top bottom (inhibit-read-only t))
  992.     (hif-find-ifdef-block) ; set top and bottom - dynamic scoping
  993.     (hide-ifdef-region top bottom)
  994.     (if hide-ifdef-lines
  995.     (progn
  996.       (hif-hide-line top)
  997.       (hif-hide-line (1+ bottom))))
  998.     (setq hide-ifdef-hiding t))
  999.   (setq buffer-read-only (or hide-ifdef-read-only hif-outside-read-only)))
  1000.  
  1001.  
  1002. (defun show-ifdef-block ()
  1003.   "Show the ifdef block (true or false part) enclosing or before the cursor."
  1004.   (interactive)
  1005.   (let ((inhibit-read-only t))
  1006.     (if hide-ifdef-lines
  1007.     (save-excursion
  1008.       (beginning-of-line)
  1009.       (hif-show-ifdef-region (1- (point)) (progn (end-of-line) (point))))
  1010.  
  1011.       (let (top bottom)
  1012.     (hif-find-ifdef-block)
  1013.     (hif-show-ifdef-region (1- top) bottom)))))
  1014.  
  1015.  
  1016. ;;;  definition alist support
  1017.  
  1018. (defvar hide-ifdef-define-alist nil
  1019.   "A global assoc list of pre-defined symbol lists")
  1020.  
  1021. (defun hif-compress-define-list (env)
  1022.   "Compress the define list ENV into a list of defined symbols only."
  1023.   (let ((defs (mapcar '(lambda (arg)
  1024.              (if (hif-lookup (car arg)) (car arg)))
  1025.               env))
  1026.     (new-defs nil))
  1027.     (while defs
  1028.       (if (car defs)
  1029.       (setq new-defs (cons (car defs) new-defs)))
  1030.       (setq defs (cdr defs)))
  1031.     new-defs))
  1032.  
  1033. (defun hide-ifdef-set-define-alist (name)
  1034.   "Set the association for NAME to `hide-ifdef-env'."
  1035.   (interactive "SSet define list: ")
  1036.   (setq hide-ifdef-define-alist
  1037.     (cons (cons name (hif-compress-define-list hide-ifdef-env))
  1038.           hide-ifdef-define-alist)))
  1039.  
  1040. (defun hide-ifdef-use-define-alist (name)
  1041.   "Set `hide-ifdef-env' to the define list specified by NAME."
  1042.   (interactive "SUse define list: ")
  1043.   (let ((define-list (assoc name hide-ifdef-define-alist)))
  1044.     (if define-list
  1045.     (setq hide-ifdef-env
  1046.           (mapcar '(lambda (arg) (cons arg t))
  1047.               (cdr define-list)))
  1048.       (error "No define list for %s" name))
  1049.     (if hide-ifdef-hiding (hide-ifdefs))))
  1050.  
  1051. (provide 'hideif)
  1052.  
  1053. ;;; hideif.el ends here
  1054.